package cn.wj.school.cloud.service;

import cn.wj.school.cloud.dao.SyUserDetailMapper;
import cn.wj.school.cloud.dao.SyUserMapper;
import cn.wj.school.cloud.dto.PageResponse;
import cn.wj.school.cloud.dto.SyUserDTO;
import cn.wj.school.cloud.dto.SyUserDetailDTO;
import cn.wj.school.cloud.dto.condition.UserCondition;
import cn.wj.school.cloud.exception.ServiceInvokeException;
import cn.wj.school.cloud.model.SyUser;
import cn.wj.school.cloud.model.SyUserDetail;
import cn.wj.school.cloud.utils.BeanCopyUtil;
import cn.wj.school.cloud.utils.EncryptAlgorithm;
import cn.wj.school.cloud.utils.SecurityUtils;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Collections;
import java.util.Date;
import java.util.List;

/**
 * @Author: yueding
 * @Description:
 * @Date: Created in 15:22 2018/4/12
 * @Modified By: Copyright(c) cai-inc.com
 */
@Service
public class UserServiceImpl implements UserService {

    @Autowired
    private SyUserMapper syUserMapper;

    @Autowired
    private SyUserDetailMapper syUserDetailMapper;

    /**
     * 通过用户登录账户获取用户信息
     *
     * @param account
     * @return
     */
    @Override
    public SyUserDTO getUserByUserAccount(String account) {
        if (StringUtils.isBlank(account)) {
            return null;
        }
        SyUser user = syUserMapper.selectByAccount(account);
        return user != null ? BeanCopyUtil.genBean(user, SyUserDTO.class) : null;
    }

    /**
     * 用户id获取用户信息
     *
     * @param userId
     * @return
     */
    @Override
    public SyUserDTO getUserByUserId(Long userId) {
        if (userId == null) {
            return null;
        }
        SyUserDTO syUserDTO = BeanCopyUtil.genBean(syUserMapper.selectByPrimaryKey(userId), SyUserDTO.class);
        syUserDTO.setDetail(
                BeanCopyUtil.genBean(syUserDetailMapper.selectByUserId(userId), SyUserDetailDTO.class));
        return syUserDTO;
    }

    /**
     * 分页查询用户信息
     *
     * @param condition
     * @return
     */
    @Override
    public PageResponse<SyUserDTO> getUsersByCondition(UserCondition condition) {
        long count = syUserMapper.countUserByCondition(condition);
        if (count > 0) {
            return new PageResponse<>(count,
                    BeanCopyUtil.genBeanList(syUserMapper.selectUsersByCondition(condition), SyUserDTO.class));
        }
        return new PageResponse<>(0, Collections.emptyList());
    }

    /**
     * 修改密码
     *
     * @param userId
     * @param password
     */
    @Override
    public void updateUserPwd(Long userId, String password) {
        if (userId == null) {
            return;
        }
        SyUser syUser = new SyUser();
        syUser.setId(userId);
        syUser.setPassword(password);
        syUserMapper.updateByPrimaryKeySelective(syUser);
    }

    /**
     * 保存用户
     *
     * @param user
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void saveUser(SyUserDTO user) {
        if (user == null) {
            throw ServiceInvokeException.newException("参数异常!");
        }
        SyUser sysUser = BeanCopyUtil.genBean(user, SyUser.class);
        sysUser.setPassword(EncryptAlgorithm.hexMD5(user.getPassword() == null ? "a123456" : user.getPassword()));
        SyUserDTO loginUser = SecurityUtils.getCurrentUser();
        sysUser.setOperator(loginUser.getUserName());
        sysUser.setCreateAt(new Date());
        sysUser.setUpdateAt(new Date());
        syUserMapper.insert(sysUser);
        SyUserDetail detail = BeanCopyUtil.genBean(user.getDetail(), SyUserDetail.class);
        detail.setUserId(sysUser.getId());
        syUserDetailMapper.insert(detail);
    }

    /**
     * 删除用户
     *
     * @param userId
     */
    @Transactional(rollbackFor = Exception.class)
    @Override
    public void removeUser(Long userId) {
        if (userId != null) {
            syUserMapper.deleteByPrimaryKey(userId);
        }
    }

    /**
     * 修改用户
     *
     * @param user
     */
    @Override
    public void modifyUser(SyUserDTO user) {
        if (user == null || user.getId() == null) {
            throw ServiceInvokeException.newException("参数异常!");
        }
        SyUser sysUser = BeanCopyUtil.genBean(user, SyUser.class);
        SyUserDTO loginUser = SecurityUtils.getCurrentUser();
        sysUser.setOperator(loginUser.getUserName());
        sysUser.setUpdateAt(new Date());
        syUserMapper.updateByPrimaryKeySelective(sysUser);
        SyUserDetail detail = BeanCopyUtil.genBean(user.getDetail(), SyUserDetail.class);
        detail.setUserId(sysUser.getId());
        if (detail.getId() == null) {
            syUserDetailMapper.insert(detail);
        }else{
            syUserDetailMapper.updateByPrimaryKeySelective(detail);
        }
    }
}
